package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 线程优化级
 * @author liubin
 */
public class Priority {

	private static volatile boolean notStart = false;
	private static volatile boolean notEnd = false;

	public static void main(String[] args) throws InterruptedException {
		List<Job> jobs = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			int priority = i < 5 ? Thread.MIN_PRIORITY : Thread.MAX_PRIORITY;
			Job job = new Job(priority);
			jobs.add(job);

			Thread thread = new Thread(job, "thread:" + i);
			thread.setPriority(priority);
			thread.start();
		}

		notStart = false;
		TimeUnit.SECONDS.sleep(10);
	}

	static class Job implements Runnable {
		private int priority = 0;
		private long jobCount = 0;

		public Job(int priority) {
			this.priority = priority;
		}

		@Override
		public void run() {
			while (notStart) {
				Thread.yield();
			}

			while (notEnd) {
				Thread.yield();
				jobCount++;
			}
		}

	}
}
